const app = getApp();
Page({
  data: {
    server:app.globalData.server,
    api:app.globalData.api,
    token:'',
    addressList:{
      total:null,
      page: 1,
      list:[],
      show: true,
    },
    flag:null,
    deleteModalOpened:false,
    deleteBtn:[
      { text: '取消' },
      { text: '删除', extClass: 'buttonBold' },
    ],
    deletedId:null,
  },
  onLoad(query) {
    console.log('loaded')
    console.log(query);
    if(query.confirmToAddress){
      this.setData({
        flag:query.confirmToAddress
      })
    }
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          token:res.data?res.data.token:'',
          addressList:{
            total:null,
            page: 1,
            list:[],
            show: true,
          },
        })
        that.mySchedulde()
      }
    });
  },
  onHide() {
    this.setData({
      flag:null
    })
  },
  mySchedulde(page = 1) {
    try {
      let url = this.data.api+'/user/addressList?page='+page;
      let list = this.data.addressList.list;
      let that = this;
      my.request({
        url:url,
        method: 'get',
        headers:{
          'content-type':'application/json',
          'token':this.data.token
        },
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.errCode==401){//token过期了
            my.removeStorage({
              key: 'userInfo'
            });
            that.setData({
              token:'',
              addressList:{
                total:null,
                page: 1,
                list:[],
                show: true,
              },
            })
            my.showToast({
              type: 'exception',
              content: res.data.message,
              duration: 2000
            });
            my.reLaunch({ url: '../login/login' });
            return false
          }
          if(res.data.errCode==0){
            let data = res.data.data.result;
            for (let i = 0; i < data.length; i++) {
              list.push(data[i]);
            }
            that.setData({
              addressList:{
                total:res.data.data.total_count,
                page: page,
                list:list,
                show: false
              },
            })
          }
        }
      });
    } catch (e) {
      console.log('mySchedulde执行异常:', e);
    }
  },
  scrollMytrip() {
    try {
      let page = this.data.addressList.page;
      let list =  this.data.addressList.list;
      // 判断是否还有数据需要加载
      if (list.length < this.data.addressList.total) {
        this.data.addressList.show = true;
        let newPage = page + 1;
        this.mySchedulde(newPage);
      }
    } catch (e) {
      this.data.addressList.show = false;
      console.log('scrollMytrip执行异常:', e);
    }
  },
  bindSelectAddress(e){
    let that = this;
    console.log(e)
    console.log(e.target.dataset.key)
    if(this.data.flag){
      my.setStorage({
        key: 'selectedAddress',
        data:e.target.dataset.key,
        success: function() {
          that.setData({
            flag:null
          })
          my.redirectTo({ url: '../confirm-order/confirm-order' })
        }
      });
    }
  },
  bindEditAddress(e){
    console.log(e.target.dataset.key);
    if(this.data.flag){//从确认订单过来选择地址的
      my.navigateTo({ url: '../add-address/add-address?index='+e.target.dataset.key+'&confirmToAddress=1' })//confirmToAddress=1从选择地址过来的
      this.setData({
        flag:null
      })
    }else{
      my.navigateTo({ url: '../add-address/add-address?index='+e.target.dataset.key })
    }
  },
  bindDeleteAddress(e){
    let cancel = e.target.dataset.index;
    if(!cancel){
      this.setData({
        deleteModalOpened: false
      })
      return false
    }
    let id = this.data.deletedId;
    let that = this;
    my.request({
      url:this.data.api+"/user/removeAddress",
      method: 'post',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      data:{
        id:id
      },
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          that.setData({
            deleteModalOpened: false
          })
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
            addressList:{
              total:null,
              page: 1,
              list:[],
              show: true,
            },
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          my.showToast({
            content: res.data.message,
            type: 'success',
            duration: 2000
          });
          that.setData({
            addressList:{
              total:null,
              page: 1,
              list:[],
              show: true,
            },
          })
          that.setData({
            deleteModalOpened: false
          })
          that.mySchedulde()
        }
      }
    });

  },
  bindOpenModal(e){
    let id = e.target.dataset.key;
    this.setData({
      deleteModalOpened: true,
      deletedId:id
    })

  }
});
